<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>医院管理</el-breadcrumb-item>
      <el-breadcrumb-item>医院列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- <div class="block">
  <span class="demonstration">请选择省份</span>
  <el-cascader
    v-model="value"
    :options="options"
    @change="handleChange"></el-cascader>
</div> -->

    <!-- 分割线 -->

    <el-divider></el-divider>

    <!-- 表单 -->
    <!-- @submit.native.prevent 阻止浏览器默认事件 -->
    <!-- .native 当成原生事件才会触发 -->
    <el-form @submit.native.prevent :inline="true" size="small" class="demo-form-inline" label-width="80px">
      <el-form-item label="医院名称">
        <el-input @keyup.native.enter="submit" v-model="name" placeholder="请输入省份或者城市名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">查询</el-button>
      </el-form-item>
    </el-form>

    <el-divider content-position="left">医院列表</el-divider>

    <!-- 呈现医院列表 -->
    <!-- 电影列表 -->
    <el-table :data="hospital.result">
      <el-table-column label="医院图片" prop="h_cover" width="120px" align="center">
        <template slot-scope="scope">
        <img :src="scope.row.h_cover" alt="" width="60px" height="40px">
      </template> 
      </el-table-column>
      <el-table-column label="医院名称" prop="h_name" align="center"></el-table-column>
      <el-table-column label="医院等级" prop="h_order" align="center"></el-table-column>
      <el-table-column label="所在省份" prop="h_province" width="120px" align="center">
        <template slot-scope="scope">
          {{ scope.row.h_province != null ? scope.row.h_province : '直辖市'}}
      </template>
      </el-table-column>
      <el-table-column label="所在城市" prop="h_city" width="80px" align="center"></el-table-column>
      <el-table-column label="详细地址" prop="h_address" align="center"></el-table-column>
      <el-table-column label="操作" width="180px" align="center">
        <template slot-scope="scope">
          <el-button size="small" type="success" icon="el-icon-check" circle></el-button>
        <el-button size="small" type="info" icon="el-icon-message" circle></el-button>
        <el-button
        @click="$router.push('/home/hospital-update/'+scope.row.h_id)"
        size="small" type="warning" icon="el-icon-edit" circle></el-button>
        <el-button
        @click="deleteItem(scope.row)"
         size="small" type="danger" icon="el-icon-delete" circle></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
    @current-change="changeCurrentPage"
     style="margin:10px"
     layout="->,total,prev,pager,next,slot,jumper"
     :total="hospital.total"
     :page-size="hospital.pagesize"
     :current-page="hospital.page"
     background></el-pagination>
  </div>
</template>

<script>
import httpApi from '@/http';

  export default {
    data() {
      return {
        name:'', // 查询关键字
        hospital: { // 保存医院数据
          page:1, // 当前页码
          pagesize:3, // 每页多少条
          total:0, // 总条目数
          result:[], // 医院数据
        },
      }
    },
    methods:{
      // 删除医院
      deleteItem(hospital){
        console.log('点击了删除：', hospital)

      this.$confirm(`此操作将删除该医院, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 执行删除业务
        httpApi.hospitalApi.delete({h_id: hospital.h_id}).then(res=>{
          console.log('删除结果：', res)
          if(res.data.code==200){
            this.queryAll()
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }
        })
      })
      },
      
      // 查询所有医院
      queryAll(){
        let params = {page:this.hospital.page,pagesize:this.hospital.pagesize}
        httpApi.hospitalApi.queryAll(params).then(res =>{
          console.log('加载医院列表',res);
          this.hospital = res.data.data
        })
      },
      // 通过省份查询医院
      queryByPNameLike(){
        httpApi.hospitalApi.queryByPNameLike({h_province:this.name}).then(res =>{
          console.log('加载该省份下的医院列表',res);
          this.hospital.result = res.data.data
        })
      },
      // 总和查询方法
      submit(){
        if(this.name.trim()){ // 有关键字
        this.queryByPNameLike()
        }else { // 没有关键字
          this.queryAll()
        }
      },
      // 改变当前页 分页器当前页码改变时执行,传入当前页码
    changeCurrentPage(page){
      console.log(page);
      this.hospital.page = page // 修改当前页
 
      this.queryAll()
      
    },
    },
    mounted(){
      // 初始化医院列表
      this.queryAll()
    },
  }
</script>

<style lang="scss" scoped>

</style>